export const routes: Record<string, Array<RouteInfo>> = {
    home: [
        {
            path: "/",
            belongTo: "home",
            name: "首页",
            displayName: "首页",
            englishName: "HOME",
            isNav: true,
        }, // 首页
    ],
    aboutUs: [
        {
            path: "/aboutUs",
            belongTo: "aboutUs",
            name: "关于我们",
            displayName: "关于我们",
            englishName: "ABOUT US",
            isNav: true,
        }, // 关于我们
        {
            path: "/aboutUs/fundAim",
            belongTo: "aboutUs",
            name: "基金宗旨",
            displayName: "基金宗旨",
            englishName: "FUND AIM",
        }, // 基金宗旨
        {
            path: "/aboutUs/fundEvaluation",
            belongTo: "aboutUs",
            name: "基金评估",
            displayName: "基金评估",
            englishName: "FUND EVALUATION",
        }, // 基金评估
        {
            path: "/aboutUs/fundPurpose",
            belongTo: "aboutUs",
            name: "基金目的",
            displayName: "基金目的",
            englishName: "FUND PURPOSE",
        }, // 基金目的
        {
            path: "/aboutUs/promotion",
            belongTo: "aboutUs",
            name: "宣传和推广",
            displayName: "宣传和推广",
            englishName: "PROMOTION",
        }, // 宣传和推广
        {
            path: "/aboutUs/scholarshipManagement",
            belongTo: "aboutUs",
            name: "奖学金管理",
            displayName: "奖学金管理",
            englishName: "SCHOLARSHIP MANAGEMENT",
        }, // 奖学金管理
        {
            path: "/aboutUs/selectionProcess",
            belongTo: "aboutUs",
            name: "评选流程",
            displayName: "评选流程",
            englishName: "SELECTION PROCESS",
        }, // 评选流程
        {
            path: "/aboutUs/workDuties",
            belongTo: "aboutUs",
            name: "工作职责",
            displayName: "工作职责",
            englishName: "WORK DUTIES",
        }, // 工作职责
        {
            path: "/aboutUs/assessmentMaterials",
            belongTo: "aboutUs",
            name: "考核材料收集",
            displayName: "考核材料收集",
            englishName: "ASSESSMENT MATERIALS",
        }, // 考核材料收集
        {
            path: "/aboutUs/contactUs",
            belongTo: "aboutUs",
            name: "联系我们",
            displayName: "联系我们",
            englishName: "CONTACT US",
        }, // 联系我们
    ],
    organization: [
        {
            path: "/organization",
            belongTo: "organization",
            name: "机构设置",
            displayName: "机构设置",
            englishName: "ORGANIZATION",
            isNav: true,
        }, // 机构设置
        {
            path: "/organization/externalAffairs",
            belongTo: "organization",
            name: "外联部",
            displayName: "外联部",
            englishName: "EXTERNAL AFFAIRS",
        }, // 外联部 43
        {
            path: "/organization/liaison",
            belongTo: "organization",
            name: "对接部",
            displayName: "对接部",
            englishName: "LIAISON",
        }, // 对接部 44
        {
            path: "/organization/supervision",
            belongTo: "organization",
            name: "督查部",
            displayName: "督查部",
            englishName: "SUPERVISION",
        }, // 督查部 45
        {
            path: "/organization/finance",
            belongTo: "organization",
            name: "财务部",
            displayName: "财务部",
            englishName: "FINANCE",
        }, // 财务部 46
        {
            path: "/organization/legalAffairs",
            belongTo: "organization",
            name: "法务部",
            displayName: "法务部",
            englishName: "LEGAL AFFAIRS",
        }, // 法务部 47
        {
            path: "/organization/publicRelations",
            belongTo: "organization",
            name: "宣传部",
            displayName: "宣传部",
            englishName: "PUBLIC RELATIONS",
        }, // 宣传部 48
    ],
    donationUpdates: [
        {
            path: "/donationUpdates",
            belongTo: "donationUpdates",
            name: "捐赠动态",
            displayName: "捐赠动态",
            englishName: "DONATION UPDATES",
            isNav: true,
        }, // 捐赠动态
        {
            path: "/donationUpdates/signingUpdates",
            belongTo: "donationUpdates",
            name: "签约动态",
            displayName: "签约动态",
            englishName: "SIGNING UPDATES",
        }, // 签约动态
        {
            path: "/donationUpdates/donationListAnnual",
            belongTo: "donationUpdates",
            name: "捐赠名单",
            displayName: "捐赠名单",
            englishName: "DONATION LIST ANNUAL",
        }, // 捐赠名单
    ],
    newsCenter: [
        {
            path: "/newsCenter",
            belongTo: "newsCenter",
            name: "新闻中心",
            displayName: "新闻中心",
            englishName: "NEWS CENTER",
            isNav: true,
        }, // 新闻中心
        {
            path: "/newsCenter/newsTrends",
            belongTo: "newsCenter",
            name: "新闻动态",
            displayName: "新闻动态",
            englishName: "NEWS TRENDS",
        }, // 新闻动态
        {
            path: "/newsCenter/planningProposal",
            belongTo: "newsCenter",
            name: "策划方案",
            displayName: "策划方案",
            englishName: "PLANNING PROPOSAL",
        }, // 策划方案
    ],
    announcementArea: [
        {
            path: "/announcementArea",
            belongTo: "announcementArea",
            name: "公示专区",
            displayName: "公示专区",
            englishName: "ANNOUNCEMENT AREA",
            isNav: true,
        }, // 公示专区
    ],
    annualReport: [
        {
            path: "/annualReport",
            belongTo: "annualReport",
            name: "年度报告",
            displayName: "年度工作报告",
            englishName: "ANNUAL WORK REPORT",
            isNav: true,
        }, // 年度工作报告
        {
            path: "/annualReport/annualWorkReport",
            belongTo: "annualReport",
            name: "年度工作报告",
            displayName: "年度工作报告",
            englishName: "ANNUAL WORK REPORT",
        }, // 年度工作报告
        {
            path: "/annualReport/annualAuditReport",
            belongTo: "annualReport",
            name: "年度审计报告",
            displayName: "年度审计报告",
            englishName: "ANNUAL AUDIT REPORT",
        }, // 年度审计报告
    ],
    messageBoard: [
        {
            path: "/messageBoard",
            belongTo: "messageBoard",
            name: "留言板",
            displayName: "留言板",
            englishName: "MESSAGE BOARD",
            isNav: true,
        }, // 留言板
    ],

    // 隐藏路由
    articleTemplate: [
        {
            path: "/articleTemplate/commonArticle",
            belongTo: "articleTemplate",
            name: "常规文章",
            isHidden: true,
        },
        {
            path: "/articleTemplate/pdfArticle",
            belongTo: "articleTemplate",
            name: "PDF文章",
            isHidden: true,
        },
    ]
};

/**
 * 导出 navs 数组，该数组是基于 routes 对象动态生成的导航信息集合。
 *
 * ### 生成原理：
 *
 * 1. `Object.values(routes)`：获取 `routes` 对象的值（即各模块的路由数组），返回一个包含所有模块路由的二维数组。
 * 2. `.flat()`：将二维数组扁平化为一维数组，使所有路由对象在同一层级内，便于统一处理。
 * 3. `.filter(route => route.englishName)`：筛选出包含 `englishName` 字段的路由对象，作为主导航项。
 *    - 该过滤条件确保仅提取具有 `englishName` 的路由对象作为主导航项，方便在导航菜单中展示主要页面。
 * 4. `.map(({ name, path }) => ({ name, path }))`：将筛选出的路由对象映射为 `NavigationInfo` 类型对象，
 *    - 只保留 `name` 和 `path` 属性，确保 `navs` 数组的元素格式符合 `NavigationInfo` 接口。
 *
 * ### 数据格式示例：
 *
 * ```typescript
 * [
 *   { name: "关于我们", path: "/aboutUs" },
 *   { name: "年度工作报告", path: "/annualReport" },
 *   { name: "公示专区", path: "/announcementArea" },
 *   { name: "捐赠动态", path: "/donationUpdates" },
 *   { name: "留言板", path: "/messageBoard" },
 *   { name: "新闻中心", path: "/newsCenter" },
 *   ......
 * ]
 * ```
 *
 * ### 用途：
 *
 * - `navs` 数组作为导航栏数据来源，用于在页面上动态生成导航菜单或列表，便于展示具有 `englishName` 的主页面入口。
 */
export const navs: { path: string; displayName: string | undefined; name: string }[] = Object.values(routes)
    .flat() // 扁平化所有模块的路由信息
    .filter(route => route.isNav) // 筛选包含 englishName 的路径
    .filter(route => !route.isHidden) // 筛选不包含 isHidden 的路由
    .map(({name, displayName, path}) => ({name, displayName, path})); // 将筛选结果映射为 NavigationInfo 格式


/**
 * 根据 belongTo 找到对应的根路由信息对象
 * @param belongTo 路由的 belongTo 属性
 * @param path 路由的 path 属性，该属性只有在belongTo为articleTemplate时才有效
 */
export function findRootRouteInfo(belongTo: string, path?: string): RouteInfo | null {
    const routeGroup = routes[belongTo];
    if (routeGroup) {
        let rootRoute: RouteInfo | undefined;
        if (belongTo === 'articleTemplate') {
            // 如果是文章模板类型，做特殊检索
            rootRoute = routeGroup.find(route => route.path === path);
        } else {
            rootRoute = routeGroup.find(route => route.belongTo === belongTo && route.path === `/${belongTo}`);
        }
        return rootRoute ? rootRoute : null;
    }
    return null;
}


/**
 * 根据给定的 name 查找对应的 path，排除 isHidden=true 的对象
 * @param name - 路由的 name 属性
 * @returns 对应的 path，如果找不到则返回 undefined
 */
export function findPathByName(name: string): string {
    const allRoutes = Object.values(routes).flat(); // 扁平化所有模块的路由信息
    const targetRoute = allRoutes.find(route => route.name === name); // 查找指定 name 且 isHidden 为 false 的路由
    return targetRoute ? targetRoute.path : '/'; // 返回对应的 path，如果找不到则返回 '/'
}

/**
 * 根据给定的 name 返回对应的 routeName
 * 目的：NuxtLink统一用路由名称跳转
 * @param name - 路由的中文名称
 * @returns 对应的 routeName
 */
export function n2r(name: string) {
    return convertPathToKebabCaseName(findPathByName(name));
}